package com.angel.controller;

import com.angel.bean.ReidJSON;
import com.angel.bean.ResourceCombination;
import com.angel.bean.Webresource;
import com.angel.service.ResourceCombinationService;
import com.angel.service.WebresourceService;
import com.angel.utils.UserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 视频列表
 */
@Controller
@RequestMapping("/listvoid")
public class ListVoidControll {

    @Resource
    private ResourceCombinationService resourceCombinationService;

    @Resource
    private WebresourceService webresourceService;

    /**
     * 返回列表页面
     * @param rebinId
     * @return
     */
    @RequestMapping("/list")
    public String listVoid(@RequestParam(value = "posion", defaultValue = "1") String posion, Integer rebinId, Model model){
       // System.out.println("-------视频------");
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(posion);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        //
        ResourceCombination resourceCombination=new ResourceCombination();
        resourceCombination.setRebinId(rebinId);
        //获取
        try {
            List<Webresource> webresourceList=new ArrayList<>();
            List<ReidJSON> reidJSONS_list=new ArrayList<>();
            ResourceCombination result=resourceCombinationService.findOne(resourceCombination);
            JSONArray jsonArray=new JSONArray(result.getReId());
            //解析
            for (int j=0 ; j<jsonArray.length() ;j++){
                JSONObject jsonObject=jsonArray.getJSONObject(j);
                ReidJSON reidJSON=new ReidJSON();
                //解析
                reidJSON.setWrid(Integer.valueOf(jsonObject.getString("resid")));
                reidJSON.setAcup(Integer.valueOf(jsonObject.getString("acupid")));
                //System.out.println("-------视频------"+reidJSON.getWrid()+"---"+reidJSON.getAcup());
                //添加
                reidJSONS_list.add(reidJSON);
            }
            //视频列表
            for (int i=0 ; i<reidJSONS_list.size() ; i++){
                Webresource webresource=new Webresource();
                webresource.setReid(reidJSONS_list.get(i).getWrid());
                Webresource webresource_result=webresourceService.findOne(webresource);
                //System.out.println("-------视频------"+webresource_result.getRname());
                webresourceList.add(webresource_result);
            }
            //分页
            PageInfo page=new PageInfo(webresourceList);
            page.setList(webresourceList);
            if (page!=null){
                model.addAttribute("emp",true);//是否查找成功
                model.addAttribute("pageInfo",page);
                model.addAttribute("rebinId",rebinId);
                model.addAttribute("userid",UserUtils.getPrincipal().getUserInfo().getId());
            }else {
                model.addAttribute("emp",false);
            }
        }catch (Exception n){
            //System.out.println("数据库错误");
        }
        //System.out.println("返回页面");
        return "list_void";
    }
}
